package com.example.a10928.weifriend.ui.task_center

import android.annotation.SuppressLint
import android.databinding.ObservableBoolean
import android.databinding.ViewDataBinding
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.a10928.weifriend.App.app
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.adapter.MemberAdapter
import com.example.a10928.weifriend.adapter.MemberTaskAdapter
import com.example.a10928.weifriend.adapter.MemberTaskAdapter.TaskItemData
import com.example.a10928.weifriend.databinding.ActivityMemberTaskListBinding
import com.example.a10928.weifriend.http.configuration.ApiException
import com.example.a10928.weifriend.ui.DatabindingBaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import kotlinx.android.synthetic.main.activity_member_task_list.*

class MemberTaskListActivity : DatabindingBaseActivity() , SwipeRefreshLayout.OnRefreshListener {
    override fun onRefresh() {
        getAdverCenter()
    }

    var data: MutableList<TaskItemData> = mutableListOf()
    lateinit var adapter: MemberTaskAdapter
    var taskType: Int = -1

    override fun bindingInitData() {
        val intent = intent.getIntExtra(SOURCE_MEMBERTASKLISTACTIVITY_TYPE, -1)
        when (intent) {
            SOURCE_TASK_COMMON -> {
                setTitleText(ThemeEnum.Back_Default, "普通会员任务")
                taskType = SOURCE_TASK_COMMON
            }
            SOURCE_TASK_VIP -> {
                setTitleText(ThemeEnum.Back_Default, "VIP会员任务")
                taskType = SOURCE_TASK_VIP
            }
            else -> {
                return
            }
        }
        swipe.setOnRefreshListener(this)
        getAdverCenter()
    }

    override fun bindingInitView(viewDataBinding: ViewDataBinding?) {
        val mBinding = viewDataBinding as ActivityMemberTaskListBinding
        adapter = MemberTaskAdapter(R.layout.item_member_task, data)
        mBinding.memberTaskRv.adapter = adapter
        mBinding.memberTaskRv.layoutManager = LinearLayoutManager(this) as RecyclerView.LayoutManager?
        var divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.line_divice_20dp)!!)
        mBinding.memberTaskRv.addItemDecoration(divider)

    }

    override fun bindingGetContentView(): Int {
        return R.layout.activity_member_task_list
    }

    @SuppressLint("CheckResult")
    private fun getAdverCenter() {
        app().bpService.getAdverCenter(app().user.userId, if (taskType == SOURCE_TASK_COMMON) {
            1
        } else {
            2
        })
                .compose(RxUtil.io2mainLoad(this))
                .subscribe(
                        { r ->
                            swipe.setRefreshing(false)
                            data.clear()
                            for (item in r.data) {
                                data.add(TaskItemData(
                                        if (item.adver_type == 1) {
                                            R.drawable.task_common
                                        } else {
                                            R.drawable.task_vip
                                        },
                                        item.ad_title,
                                        if (item.is_get != 0) {
                                            "已领取"
                                        } else {
                                            "未领取"
                                        },
                                        item.commission,
                                        TaskDetailsActivity.SOURCE_TAKE_TEH_TASK,
                                        item.ad_id,
                                        if(item.surplus_number == 0){true}else{false},false))
                            }
                            adapter!!.setNewData(data)
                        }, {
                    swipe.setRefreshing(false)
                    if (it is ApiException){
                    if((it as ApiException).code==4001){
                        L.t((it as ApiException).msg)
                    }else{
                        RxUtil.errorException(it)
                    }
                } }
                )
    }


    companion object {
        val SOURCE_MEMBERTASKLISTACTIVITY_TYPE: String = "sourceMembertasklistactivityType"
        val SOURCE_TASK_COMMON = 1001
        val SOURCE_TASK_VIP = 1002
    }
}